package com.lottery.ruok.lottrey.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.adapter.E164Adapter;
import com.lottery.ruok.lottrey.retrofitModule.RetrofitBase;
import com.lottery.ruok.lottrey.retrofitModule.RxHelper;
import com.lottery.ruok.lottrey.retrofitModule.RxSubscribe;
import com.lottery.ruok.lottrey.retrofitModule.request.GetCountryRequest;
import com.lottery.ruok.lottrey.retrofitModule.response.GetCountryResponse;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.Callable;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import ui.SimpleSideBar;
/*
* 国家与地区
* */

public class ChooseE164Activity extends BaseActivity {
    @BindView(R.id.listview)
    ListView listview;
    private GetCountryResponse getCountryResponse;
    E164Adapter myAdapter;
    @BindView(R.id.sideBar)
    SimpleSideBar sideBar;

    public static Bitmap chooseFlag;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_e164);
        ButterKnife.bind(this);

        init();
    }

    private void init() {
        getList();

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                chooseFlag = getCountryResponse.getList().get(i).getBitmap();

                Intent intent = new Intent();
                intent.putExtra("e164", getCountryResponse.getList().get(i).getCode());
                setResult(RESULT_OK, intent);

                finish();
            }
        });

        sideBar.setOnLetterTouchedChangeListener(new SimpleSideBar.OnLetterTouchedChangeListener() {
            @Override
            public void onTouchedLetterChange(String letterTouched) {
                if(myAdapter != null) {
                    int pos = myAdapter.getLetterPosition(letterTouched);
                    if (pos != -1) {
                        listview.setSelection(pos);
                    }
                }
            }
        });
    }

    private void getFlagImage(){
        Observable.fromCallable(new Callable<Bitmap>() {
            @Override
            public Bitmap call() {
                try {
                    URL url = new URL(getCountryResponse.getImage());
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    InputStream is = conn.getInputStream();
                    return BitmapFactory.decodeStream(is);
                } catch (IOException e) {
                    e.printStackTrace();
                    return null;
                }
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Bitmap>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Bitmap bitmap) {
                        for(int i = 0; i < getCountryResponse.getList().size(); i++){
                            getCountryResponse.getList().get(i).setBitmap(Bitmap.createBitmap(bitmap, -Integer.parseInt(getCountryResponse.getList().get(i).getPosition()), 0, getCountryResponse.getList().get(i).getWidth(), 15));
                        }
                        myAdapter = new E164Adapter(ChooseE164Activity.this, getCountryResponse);
                        listview.setAdapter(myAdapter);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void getList() {
        RetrofitBase.getRetrofit().create(GetCountryRequest.class).getResult()
                .compose(RxHelper.<GetCountryResponse>handleResult())
                .subscribe(new RxSubscribe<GetCountryResponse>(this) {
                    @Override
                    protected void _onNext(GetCountryResponse g) {
                        getCountryResponse = g;

                        if(getCountryResponse != null){
                            for(GetCountryResponse.ListBean listBean : getCountryResponse.getList()){
                                listBean.setFisrtLetter(listBean.getCountry().substring(0, 1).toUpperCase());
                            }

                            getFlagImage();
                        }
                    }

                    @Override
                    protected boolean showDialog() {
                        return true;
                    }
                });
    }
}